package cn.zwz.bus.controller;

import cn.zwz.basics.baseVo.PageVo;
import cn.zwz.basics.baseVo.Result;
import cn.zwz.basics.utils.PageUtil;
import cn.zwz.basics.utils.ResultUtil;
import cn.zwz.bus.entity.BasicBusiness;
import cn.zwz.bus.service.IBasicBusinessService;
import cn.zwz.data.utils.ZwzNullUtils;
import cn.zwz.data.vo.AntvVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author 郑为中
 * CSDN: Designer 小郑
 */
@Slf4j
@RestController
@Api(tags = "基础业务管理接口")
@RequestMapping("/zwz/basicBusiness")
@Transactional
public class BasicBusinessController {

    @Autowired
    private IBasicBusinessService iBasicBusinessService;

    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ApiOperation(value = "查询单条基础业务")
    public Result<BasicBusiness> get(@RequestParam String id){
        return new ResultUtil<BasicBusiness>().setData(iBasicBusinessService.getById(id));
    }

    @RequestMapping(value = "/count", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部基础业务个数")
    public Result<Long> getCount(){
        return new ResultUtil<Long>().setData(iBasicBusinessService.count());
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部基础业务")
    public Result<List<BasicBusiness>> getAll(){
        return new ResultUtil<List<BasicBusiness>>().setData(iBasicBusinessService.list());
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "查询基础业务")
    public Result<IPage<BasicBusiness>> getByPage(@ModelAttribute BasicBusiness basicBusiness ,@ModelAttribute PageVo page){
        QueryWrapper<BasicBusiness> qw = new QueryWrapper<>();
        if(!ZwzNullUtils.isNull(basicBusiness.getType())) {
            qw.eq("type",basicBusiness.getType());
        }
        if(!ZwzNullUtils.isNull(basicBusiness.getTitle())) {
            qw.like("title",basicBusiness.getTitle());
        }
        IPage<BasicBusiness> data = iBasicBusinessService.page(PageUtil.initMpPage(page),qw);
        return new ResultUtil<IPage<BasicBusiness>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "增改基础业务")
    public Result<BasicBusiness> saveOrUpdate(BasicBusiness basicBusiness){
        if(iBasicBusinessService.saveOrUpdate(basicBusiness)){
            return new ResultUtil<BasicBusiness>().setData(basicBusiness);
        }
        return ResultUtil.error();
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "新增基础业务")
    public Result<BasicBusiness> insert(BasicBusiness basicBusiness){
        iBasicBusinessService.saveOrUpdate(basicBusiness);
        return new ResultUtil<BasicBusiness>().setData(basicBusiness);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "编辑基础业务")
    public Result<BasicBusiness> update(BasicBusiness basicBusiness){
        iBasicBusinessService.saveOrUpdate(basicBusiness);
        return new ResultUtil<BasicBusiness>().setData(basicBusiness);
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "删除基础业务")
    public Result<Object> delByIds(@RequestParam String[] ids){
        for(String id : ids){
            iBasicBusinessService.removeById(id);
        }
        return ResultUtil.success();
    }

    @RequestMapping(value = "/getAntvVoList", method = RequestMethod.GET)
    @ApiOperation(value = "查询图表数据")
    public Result<List<AntvVo>> getAntvVoList(){
        List<AntvVo> ansList = new ArrayList<>();
        List<BasicBusiness> teacherList = iBasicBusinessService.list();
        for (BasicBusiness o : teacherList) {
            boolean flag = false;
            for (AntvVo vo : ansList) {
                if(Objects.equals(vo.getTitle(),o.getTitle())) {
                    flag = true;
                    vo.setValue(vo.getValue().add(BigDecimal.ONE));
                    break;
                }
            }
            if(!flag) {
                AntvVo vo = new AntvVo();
                vo.setTitle(o.getTitle());
                vo.setType("类型个数");
                vo.setValue(BigDecimal.ONE);
                ansList.add(vo);
            }
        }
        return new ResultUtil<List<AntvVo>>().setData(ansList);
    }
}
